/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:17:32
 * @Last Modified by: songwh
 * @Last Modified time: 2018-04-04 15:04:11
*/

import { modal } from 'vue-strap'
import { addBuyerItem, showMsg } from '../../vuex/action'

export default {
  name: 'buyer-config',
  data() {
    return {
      showModal: false,
      actionType: 'ADD', // ADD or UPDATE
      form: {
        name: '',
        color: '#000000'
      }
    }
  },
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xCreate: addBuyerItem,
      xShowMsg: showMsg
    }
  },
  methods: {
    valid() {
      let {name, color } = this.form
      for (let v of [name, color]) {
        if ((v + '').length === 0) {
          return Promise.reject({
            error_msg: '* 标记的均为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          let {name, color} = this.form
          let params = {
            name: name,
            color: color,
          }
          return this.xCreate(params)
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
